import {createRouter,createWebHashHistory} from 'vue-router'
import store from '@/store'
import { menuTree } from '@/apis/personal'


// const Home = ()=> import("@/views/Home.vue")

// 1. layout引入
import Layout from '@/layout/Index.vue'

// 页面
const Login = () => import("@/views/login/Index.vue")
const PersonalIndex = () => import("@/views/personal/Index.vue")
const PersonalMessage = () => import("@/views/personal/Message.vue")
// const AppIndex = () => import("@/views/app/Index.vue")

// const NotFound = () => import("@/views/404.vue")

// 应用
const AppDept = () => import('@/views/app/Dept.vue')
const AppResource = () => import('@/views/app/Resource.vue')
const AppRole = () => import('@/views/app/Role.vue')
const AppUser = () => import('@/views/app/User.vue')


// 系统
const SysNotice = () => import('@/views/sys/Notice.vue')
const SysUser = () => import('@/views/sys/User.vue')

// 审计
const LogsOperation = () => import('@/views/logs/Operation.vue')
const LogsVisit = () => import('@/views/logs/Visit.vue')

const routes = [
    {
        path: '/',
        name: 'Home',
        component: Layout,
        children: [
            {
                path: 'login',
                name: 'Login',
                component: Login
            },
            {
                path: 'personal',
                name: 'Personal',
                component: PersonalIndex,
                children: [
                    {
                        path: 'message',
                        name: 'PersonalMessage',
                        component: PersonalMessage
                    },
                ]
            },
            // 应用 路由
            {
                path: '/app',
                name: 'App',
                children: [
                    {
                        path: 'user',
                        name: 'AppUser',
                        component: AppUser
                    },
                    {
                        path: 'dept',
                        name: 'AppDept',
                        component: AppDept
                    },
                    {
                        path: 'role',
                        name: 'AppRole',
                        component: AppRole
                    },
                    {
                        path: 'resource',
                        name: 'AppResource',
                        component: AppResource
                    },
                ]
            },
            // 系统 路由
            {
                path: '/sys',
                name: 'Sys',
                children: [
                    {
                        path: 'user',
                        name: 'SysUser',
                        component: SysUser
                    },
                    {
                        path: 'notice',
                        name: 'SysNotice',
                        component: SysNotice
                    },
                ]
            },
            // 审计 路由
            {
                path: '/logs',
                name: 'Logs',
                children: [
                    {
                        path: 'visit',
                        name: 'LogsVisit',
                        component: LogsVisit
                    },
                    {
                        path: 'operation',
                        name: 'LogsOperation',
                        component: LogsOperation
                    },
                ]
            },
            // {
            //     path: '404',
            //     name: 'NotFound',
            //     component: NotFound
            // }
        ]
    },
]


const router = createRouter({
    history: createWebHashHistory(),
    routes
})




// 路由拦截守卫
router.beforeEach(async (to) => {

    // 判断是否登录
    const isLogin = store.getters['user/isLogin']

    if (to.path === '/login') {
        if (isLogin) {
            return store.state.firstRoute
        }
        return true
    }

    // 判断菜单是否加载
    if (store.state.routeLoaded) {
        return true
    }

    // 菜单的初始化
    await initMenu()
    const { state: { firstRoute } } = store
    if (to.path === '/' && firstRoute) {
        return firstRoute
    }
    return true
})

// 菜单
function initMenu() {
    return menuTree()
        .then((res) => {
            const { data } = res
            store.commit('setRouteLoaded', true)
            store.commit('setMenuTree', data)
            store.commit('setFirstRoute', data[0].children[0].path)
        })
}

export default router

